  /******************************************************************************
  * @file    PT32X005_uart.h
  * @author  应用开发团队
  * @version V1.1.0
  * @date    2020/1/27
  * @brief    This file contains all the functions prototypes for the UART firmware library.
  *
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef PT32X005_UART_H
#define PT32X005_UART_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "PT32X005.h"


/** @addtogroup UART
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/** @brief UART Init Structure definition **/
typedef struct
{
	u32 UART_BaudRate;
	u16 UART_WordLength;
	u16 UART_StopBits;
	u16 UART_Parity;
	u16 UART_Mode;
	u16 UART_LoopBack;
} UART_InitTypeDef;


/** @defgroup UART_FIFO_TYPE **/
typedef enum
{
	Rx_FIFO,
	Tx_FIFO
} UART_FifoTypeDef;


/* Exported constants --------------------------------------------------------*/

/** @defgroup UART_Oversampling_Rate **/
#define IS_UART_ALL_PERIPH(PERIPH)   ((PERIPH) == UART0) || ((PERIPH) == UART1)

#define IS_UART_BR_VAL(Value)	(((Value) > 46) && ((Value) <= 3000000))

#define IS_UART_RE_SEL(SEL)		(((SEL) == UART_RE_Enable) || \
								((SEL) == UART_RE_Disable))

#define UART_LoopBack_Disable		((u16)0x0000)
#define UART_LoopBack_Enable		UART_CR_LPB
#define IS_UART_LoopBack_SEL(SEL)	(((SEL) == UART_LoopBack_Disable) || \
									((SEL) == UART_LoopBack_Enable))

#define UART_IrDAPolarity_Norm		    (~UART_IRC_IRPN)
#define UART_IrDAPolarity_Reverse	    UART_IRC_IRPN
#define IS_UART_IrDAPolarity_SEL(SEL)	(((SEL) == UART_IrDAPolarity_Norm) || \
										((SEL) == UART_IrDAPolarity_Reverse))

/** @defgroup UART_Global_definition **/
#define IS_UART_DATA(DATA)             ((DATA) <= 0x1FF)
#define IS_UART_DUTYCYCLE(DUTYCYCLE)   ((DUTYCYCLE) <= 0x00004095)


/** @defgroup IS_UART_WP_SEL **/
#define IS_UART_WP_SEL(SEL)	(((SEL) == UART_WP_8D) || \
								((SEL) == UART_WP_7D1P) || \
								((SEL) == UART_WP_9D) || \
								((SEL) == UART_WP_8D1P))

/** @defgroup IS_UART_STOP_SEL **/
#define IS_UART_STOP_SEL(SEL)	(((SEL) == UART_STOP_05b) || \
								((SEL) == UART_STOP_1b) || \
								((SEL) == UART_STOP_15b) || \
								((SEL) == UART_STOP_2b))

/** @defgroup IS_UART_PS_SEL **/
#define IS_UART_PS_SEL(SEL)		(((SEL) == UART_PS_ODD) || \
								((SEL) == UART_PS_EVEN))


/** @defgroup IS_UART_OWD_SEL **/
#define IS_UART_OWD_SEL(SEL)	(((SEL) == UART_OWD_TX) || \
								((SEL) == UART_OWD_TX))


/** @defgroup IS_UART_SR_SEL **/
#define IS_UART_SR_SEL(SEL)		(((SEL) == UART_SR_RXNE)   || \
								((SEL) == UART_SR_RXF)    || \
								((SEL) == UART_SR_PE)    || \
								((SEL) == UART_SR_FE)   || \
								((SEL) == UART_SR_OVR) || \
								((SEL) == UART_SR_TXE)    || \
								((SEL) == UART_SR_TXF)    || \
								((SEL) == UART_SR_TXO))

/** @defgroup IS_UART_IE_SEL&GET **/
#define IS_UART_IE_SEL(SEL)		((((SEL) & (u16)0xF000) == 0) && ((SEL) != (u16)0x00))
#define IS_UART_IE_GET(SEL)		(((SEL) == UART_IE_RXNEI)  || \
								((SEL) == UART_IE_RXFI)   || \
								((SEL) == UART_IE_PEI) || \
								((SEL) == UART_IE_FEI)  || \
								((SEL) == UART_IE_OVRI) || \
								((SEL) == UART_IE_TXEI)   || \
								((SEL) == UART_IE_TXFI)   || \
								((SEL) == UART_IE_TXOI))

/** @defgroup IS_UART_IE_SEL&GET **/
#define IS_UART_IT_SEL(SEL)		((((SEL) & (u16)0xF000) == 0) && ((SEL) != (u16)0x00))
#define IS_UART_IT_GET(SEL)		(((SEL) == UART_SR_RXNE)   || \
								((SEL) == UART_SR_RXF)    || \
								((SEL) == UART_SR_PE)    || \
								((SEL) == UART_SR_FE)   || \
								((SEL) == UART_SR_OVR) || \
								((SEL) == UART_SR_TXE)    || \
								((SEL) == UART_SR_TXF)    || \
								((SEL) == UART_SR_TXO))


/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void UART_Init(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct);
void UART_StructInit(UART_InitTypeDef* UART_InitStruct);
void UART_Cmd(UART_TypeDef* UARTx, FunctionalState NewState);
void UART_SendData(UART_TypeDef* UARTx, u16 Data);
void UART_StringSend(UART_TypeDef* UARTx, u8 *String);
u16 UART_ReceiveData(UART_TypeDef* UARTx);


void UART_FifoReset(UART_TypeDef * UARTx,  UART_FifoTypeDef FIFO);
void UART_IrDACmd(UART_TypeDef* UARTx, FunctionalState NewState);
void UART_IrDAPolarityConfig(UART_TypeDef* UARTx, u16 Polarity);
void UART_SetIrDADutyCycle(UART_TypeDef* UARTx, u16 UART_DutyCycle);
void UART_HalfDuplexCmd(UART_TypeDef* UARTx, FunctionalState NewState);
void UART_HalfDuplexDirConfig(UART_TypeDef* UARTx, u32 UART_OWD_SEL);
void UART_ITConfig(UART_TypeDef* UARTx, u32 UART_IT, FunctionalState NewState);
FlagStatus UART_GetFlagStatus(UART_TypeDef* UARTx, u32 UART_FLAG);
void UART_ClearFlag(UART_TypeDef* UARTx, u32 UART_CLEAR_FLAG);
ITStatus UART_GetITStatus(UART_TypeDef* UARTx, u32 UART_IT);

/**
  * @}
  */


#ifdef __cplusplus
}
#endif

#endif


